from flask import request, render_template
from flask import jsonify
from flask import flash

from helper.lib.BookViewModel import BookView
from . import web
from jinja2 import *


@web.route('/book/search', methods=['GET'])
def search():
    from app.request.searchBookRequest import SearchForm
    result=[]
    form = SearchForm(request.args)
    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        from helper.lib.BookViewModel import BookView
        from yushu_book import get_book
        data = get_book(q, page)
        result = BookView.book_view(data, q)
        return render_template('search_result.html', books=result)
    else:
        flash('请输入合法关键词')
        return render_template('search_result.html', books=result)


@web.route('/book/<isbn>/detail', methods=['GET'])
def book_detail(isbn):
    from yushu_book import get_book
    data = get_book(isbn)
    result = BookView.signal_book(data)
    wishes = {'total': 0}
    gifts = {'total': 0}
    return render_template('book_detail.html', book=result, wishes=wishes, gifts=gifts)
